//
//  DXToobarView.swift
//  weiBoOfSwift
//
//  Created by simon on 15/12/10.
//  Copyright © 2015年 simon. All rights reserved.
//

import UIKit

class DXToobarView: UIView {
    
    var retweetBtn:UIButton?
    var commentsBtn:UIButton?
    var attitudesBtn:UIButton?
    
    var statuesViewModel:DXStatuesViewModel?{
    
        didSet{
    retweetBtn?.setTitle(statuesViewModel?.repostString, forState: .Normal)
    commentsBtn?.setTitle(statuesViewModel?.commentString, forState: .Normal)
    attitudesBtn?.setTitle(statuesViewModel?.attitudeString, forState: .Normal)
        
        }
    
    }  
    override init(frame: CGRect) {
    super.init(frame: frame)
    setUpUI()
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    添加子控件
    private func setUpUI(){
     retweetBtn = getBtn("timeline_icon_retweet", title: "转发")
       addSubview(retweetBtn!)
   commentsBtn = getBtn("timeline_icon_comment", title: "评论")
        addSubview(commentsBtn!)
    attitudesBtn =  getBtn("timeline_icon_unlike", title: "赞")
        addSubview(attitudesBtn!)
        addSubview(separateLine1)
         addSubview(separateLine2)
    
//        约束子控件
        retweetBtn?.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(commentsBtn!)
        }
        commentsBtn?.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(retweetBtn!.snp_trailing)
            make.top.equalTo(retweetBtn!)
            make.bottom.equalTo(retweetBtn!)
            make.width.equalTo(attitudesBtn!)
        }
        attitudesBtn?.snp_makeConstraints(closure: { (make) -> Void in
             make.leading.equalTo(commentsBtn!.snp_trailing)
             make.top.equalTo(commentsBtn!)
             make.bottom.equalTo(commentsBtn!)
             make.trailing.equalTo(self)
        })
        
          separateLine1.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(attitudesBtn!)
            make.leading.equalTo(commentsBtn!)
        }
        separateLine2.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(attitudesBtn!)
            make.leading.equalTo(attitudesBtn!)
        }

        
    }
    

//    懒加载子控件
    lazy var separateLine1:UIImageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
    
    lazy var separateLine2:UIImageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
    
    
    
    private func getBtn(imgName:String,title:String)->UIButton{
    
        let btn = UIButton(type: UIButtonType.Custom)
        btn.setImage(UIImage(named:imgName), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "timeline_card_top_background_highlighted"), forState: .Highlighted)
         btn.setBackgroundImage(UIImage(named: "timeline_card_top_background"), forState: .Normal)
        btn.setTitle(title, forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
//        btn.sizeToFit()
        return btn
    }
    
    
    
}
